package com.douwong.student.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.douwong.student.application.BaseApplication;
import com.douwong.student.model.FileManager;
import com.douwong.student.enums.FileMsgType;

import java.util.ArrayList;

public class ClassShareFileDBService extends BDBaseService<FileManager> {

//	private static ClassShareFileDBService classShareFileDBService = null;

	public static ClassShareFileDBService getInstance(Context context) {
//		if (classShareFileDBService == null) {
//			classShareFileDBService = new ClassShareFileDBService(context);
//		}
		return new ClassShareFileDBService(context);
	}

	protected ClassShareFileDBService(Context context) {
		super(context);
	}

	public void addLoaded(FileManager fileManager){
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL(
				"insert into  class_share_loaded (fileSize,fileName,sendId,creatTime,userId,filePath) values(?,?,?,?,?,?)",
				new Object[] {
						String.valueOf(fileManager.Length),
						fileManager.FileName,
						fileManager.SendID,
						fileManager.CreateTime,
						BaseApplication.sUserBean.getUserbean().getEuNumber(),
						
						fileManager.filePath
						});
		Log.e("插入数据结束", "插入数据结束");
		db.close();
	}
	public void deleteAll(){
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL(
				"delete from class_share_file where userId=?",
				new Object[] {BaseApplication.sUserBean.getUserbean().getEuNumber() });
		db.close();
	}
	public void deleteShareFile(String sendID){
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL(
				"delete from class_share_file where userId=? and sendId=?",
				new Object[] {BaseApplication.sUserBean.getUserbean().getEuNumber(),sendID });
		db.close();
	}
	@Override
	public void add(FileManager fileManager) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Log.e("插入数据开始", "插入数据开始");
		db.execSQL(
				"insert into  class_share_file (fileName,upLoader,fileSize,sendId,creatTime,userId,port,partCount,isShare,DataType) values(?,?,?,?,?,?,?,?,?,?)",
				new Object[] {
						fileManager.FileName,
						fileManager.SendName,
						String.valueOf(fileManager.Length),
						fileManager.SendID,
						fileManager.CreateTime,
						BaseApplication.sUserBean.getUserbean().getEuNumber(),
						fileManager.getPort(),
						fileManager.getPartCount(),
						fileManager.IsShare == true ? 1 : 0,
						fileManager.getDataType() == FileMsgType.RegularFile ? 0
								: 1 });
		Log.e("插入数据结束", "插入数据结束");
		db.close();
	}

	// public void delete(FileManager fileManager){
	// db=dbHelper.getWritableDatabase();
	// db.execSQL("delete from class_share_file where userId=? and sendId=?",new
	// String[]{MyApp.student.getUserId(),classShareInfor.getSendId()});
	// db.close();
	// }
	@Override
	public void delete(int id) {

	}

	public void upDate(FileManager fileManager) {
		Log.e("upDate", "开始更新");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		try {
			db.execSQL(
					"update class_share_file set isLoading=?,isPause=?,isLoaded=?,currentPro=?,filePath=?,isExist=? where userId=? and sendId=?",
					new Object[] { fileManager.isLoading == true ? 1 : 0,
							fileManager.isPause == true ? 1 : 0,
							fileManager.isLoaded == true ? 1 : 0,
							String.valueOf(fileManager.getCurrentPro()),
							fileManager.getFilePath(),
							fileManager.isExist == true ? 1 : 0,
							BaseApplication.sUserBean.getUserbean().getEuNumber(),
							fileManager.SendID, });
		} catch (Exception e) {
			e.printStackTrace();
		}
		db.close();
	}

	@Override
	public void update(int id) {

	}

	@Override
	public FileManager query(int id) {
		return null;
	}
    public void deleteLoaded(String sendId ){
    	SQLiteDatabase db = dbHelper.getWritableDatabase();
    	db.execSQL(
				"delete from class_share_loaded where sendId=? and userId=?",
				new Object[] { sendId,BaseApplication.sUserBean.getUserbean().getEuNumber() });
    	db.close();
    }
	// 查询已下载的文件
	public ArrayList<FileManager> loadedList(ArrayList<FileManager> loadedList) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(
				"select * from class_share_loaded where userId=? order by id desc",
				new String[] { BaseApplication.sUserBean.getUserbean().getEuNumber() });
		if (cursor != null) {
			while (cursor.moveToNext()) {
				Log.e("有数据", "有数据");
				FileManager fileManager = new FileManager();
				fileManager.FileName = cursor.getString(2);
				fileManager.filePath = cursor.getString(5);
				fileManager.CreateTime = cursor.getString(3);
				fileManager.SendID = cursor.getString(6);
				fileManager.Length = Integer.valueOf(cursor.getString(1));
				loadedList.add(fileManager);
			}
		}
		cursor.close();
		db.close();
		return loadedList;

	}

	// 查询正在下载的文件
	public ArrayList<FileManager> loadingList(ArrayList<FileManager> loadingList) {
		Log.e("loadingList", "查询正在下载的文件");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select * from class_share_file where isLoading=1 and userId=?",
						new String[] { BaseApplication.sUserBean.getUserbean().getEuNumber() });
		if (cursor != null) {
			while (cursor.moveToNext()) {
				Log.e("正在下载有数据", "正在下载有数据");
				FileManager fileManager = new FileManager();
				fileManager.setFileName(cursor.getString(3));
				fileManager.setLength(Integer.valueOf(cursor.getString(2)));
				fileManager.setCreateTime(cursor.getString(4));
				fileManager.setUpLoader(cursor.getString(1));
				fileManager.setPause(cursor.getInt(9) == 1 ? true : false);
				fileManager.setLoaded(cursor.getInt(8) == 1 ? true : false);
				fileManager.setLoading(cursor.getInt(7) == 1 ? true : false);
				fileManager.setExist(cursor.getInt(16) == 1 ? true : false);
				fileManager.setFilePath(cursor.getString(11));
				fileManager.setCurrentPro(Integer.valueOf(cursor.getString(5)));
				fileManager.setPort(cursor.getInt(12));
				fileManager.setIsShare(cursor.getInt(14) == 1 ? true : false);
				fileManager
						.setDataType(cursor.getInt(15) == 0 ? FileMsgType.RegularFile
								: FileMsgType.ChatFile);
				fileManager.setPartCount(cursor.getInt(13));
				fileManager.setSendID(cursor.getString(6));
				Log.e("fileManager", fileManager.getSendID() + "");
				loadingList.add(fileManager);
			}
		}
		cursor.close();
		db.close();
		return loadingList;

	}

	// 查询课堂共享文件
	public ArrayList<FileManager> classShareList(
			ArrayList<FileManager> classShareList) {
		classShareList.clear();
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(
				"select * from class_share_file where userId=?",
				new String[] { BaseApplication.sUserBean.getUserbean().getEuNumber() });
		if (cursor != null) {
			while (cursor.moveToNext()) {
				FileManager fileManager = new FileManager();
				fileManager.setFileName(cursor.getString(3));
				fileManager.setFilePath(cursor.getString(11));
				fileManager.setLength(Integer.valueOf(cursor.getString(2)));
				fileManager.setCreateTime(cursor.getString(4));
				fileManager.setUpLoader(cursor.getString(1));
				fileManager.setPort(cursor.getInt(12));
				fileManager.setIsShare(cursor.getInt(14) == 1 ? true : false);
				fileManager
						.setDataType(cursor.getInt(15) == 0 ? FileMsgType.RegularFile
								: FileMsgType.ChatFile);
				fileManager.setPartCount(cursor.getInt(13));
				fileManager.setLoaded(cursor.getInt(8) == 1 ? true : false);
				fileManager.setLoading(cursor.getInt(7) == 1 ? true : false);
				fileManager.setSendID(cursor.getString(6));
				classShareList.add(fileManager);
			}
		}
		cursor.close();
		db.close();
		return classShareList;

	}

}
